<template>
    <div class="goodsinfo-container">

        <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter">
            <div class="ball" v-if='isShow' ref="ball"></div>
        </transition>

        <!-- 轮播图区域 使用封装好的swiper组件 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <!-- 封装好的轮播图组件 -->
                    <swiper :imgs='lunbotu' :isFull="false"></swiper>
                </div>
            </div>
        </div>

        <!-- 商品购买区域 -->
        <div class="mui-card">
            <div class="mui-card-header">{{ goodsinfo.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价：<del>￥{{ goodsinfo.market_price }}</del>&nbsp;&nbsp;销售价：<span class="now_price">￥{{ goodsinfo.sell_price }}</span>
                    </p>
                    <p class="num"><span>购买数量:</span><numbox @getcount="getSelectedCount" :max="goodsinfo.stock_quantity"></numbox></p>
                    <p>
                        <mt-button type='primary' size="small">立即购买</mt-button>
                        <mt-button type='danger' size="small" @click="goShopCar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>

        <!-- 商品参数区域 -->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{ goodsinfo.goods_no }}</p>
                    <p>库存情况：{{ goodsinfo.stock_quantity }}件</p>
                    <p>上架时间：{{ goodsinfo.add_time | dateFormat }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goGoodsDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)" >商品评论</mt-button>

            </div>
        </div>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui'
    import swiper from '../subcomponents/swiper.vue'
    //导入数字文本框
    import numbox from '../subcomponents/goodsinfo_numbox.vue'


    export default{
        data(){
            return{
                id : this.$route.params.id,
                lunbotu:[],
                goodsinfo:{},//获取商品的信息
                isShow:false,
                selectedCount:1//被选中商品的数量,默认为1

            }
        },
        created(){
            this.getLunbotu();
            this.getGoodsInfo();
        },
        methods:{
            getLunbotu(){
                this.axios
                .get('api/getthumimages/'+this.id)
                .then(result => {
                     if(result.data.status === 0){
                        // swiper子组件中<img :src="item.img" >
                        result.data.message.forEach(item=>{
                            item.img=item.src;
                        })
                        this.lunbotu = result.data.message ;
                    }else{
                        Toast("获取轮播图失败");
                    }
                })
            },
            getGoodsInfo(){//获取商品信息
                this.axios
                .get("api/goods/getinfo/" + this.id)
                .then(result=>{
                    if(result.data.status === 0){
                        this.goodsinfo = result.data.message[0]
                    }else{
                        Toast("获取商品详情失败");
                    }
                })
            },
            goGoodsDesc(id){//点击使用编程式导航跳转到图文详情
                this.$router.push('/home/goodsdesc/' + id)
            },
            goComment(id){//跳转到评论
                this.$router.push('/home/goodscomment/' + id)
            },
            goShopCar(){//添加到购物车
                this.isShow = !this.isShow;
                //拼接出一个要保存到store中cart数组中的商品信息对象
                var selectedInfo = {
                    id:this.id,
                    count: this.selectedCount,
                    price:this.goodsinfo.sell_price,
                    selected:true
                }

                this.$store.commit('AddGoodsInfo',selectedInfo)
            },
            beforeEnter(el){
                el.style.transform = 'translate(0,0)';
            },
            enter(el,done){
                el.offsetWidth;//不写没有动画效果

                /*translate的位移不能写固定值，当滚动条滚动或者分辨率不同时，小球不能正常到达购物车的位置上
                我们可以使用object.getBoundingClientRect();获取元素相对于视窗的位置的集合*/
                const ballPosition = this.$refs.ball.getBoundingClientRect();
                var badge = document.getElementById('badge');
                const badgePosition = badge.getBoundingClientRect();

                const xDist = badgePosition.left - ballPosition.left;
                const yDist = badgePosition.top - ballPosition.top;
                el.style.transform = `translate(${xDist}px,${yDist}px)`;
                el.style.transition = 'all .5s cubic-bezier(.24,.71,.63,.97)';

                done();
            },
            afterEnter(el){
                this.isShow=false;
            },
            getSelectedCount(count){//子组件向父组件传递购买商品的数量，父组件把该值保存到data上
                this.selectedCount = count;
                console.log("父组件拿到的count值为："+this.selectedCount);
            }

        },
        components:{
            swiper,
            numbox
        }
    }
</script>

<style lang="scss" scoped>
.goodsinfo-container{
    background-color:#eee;
    overflow:hidden;

    .now_price{
        color:red;
        font-size:16px;
        font-weight:bold;
    }

    .mui-card-footer{
        display:block;

        button{
            margin:15px 0;
        }
    }

    .num{
        display:flex;
        span{
            line-height:33px;
        }
    }

    .ball{
        position: absolute;
        z-index:11;
        top:407px;
        left:140px;
        width:15px;
        height:15px;
        background-color: red;
        border-radius:50%;
    }
}
</style>

